'use strict';
const requireEx =require('yi-require');

const middleware =(req, res, next)=>{
    let doc = req.body;
    req.mongo.collection('goodsType').find({_id:doc._id}).next((e0,r0)=>{
        if(e0) return res.err.mongoError(e0);
        if(r0.sort!==doc.sort){
            req.mongo.collection('goodsType').updateOne({storeId:r0.storeId,sort:doc.sort},{$set:{sort:r0.sort}},(e2,r2)=>{
                console.log(r0.storeId+'*******************'+doc.sort);
                if(e2) return res.err.mongoError(e2);
                req.mongo.collection('goodsType').updateOne({_id:doc._id},{$set:{sort:doc.sort}},(e1,r1)=>{
                    if(e1) return res.err.mongoError(e0);
                });
            });
        }
        next();
    });
};

module.exports = {
    open:false,
    middleware:middleware,
    description: '更新商品',
    handler: requireEx('yi-restful').u('goodsType', [])
}